import 'package:znmm/UI/utils/my_global.dart';

class AttributesModel {
  String label = "";
  String name = "";

  List<String> options = [];

  AttributesModel({
    this.label = "",
    this.name = "",
    this.options = const [],
  });

  AttributesModel.fromJson(Map<String, dynamic> json) {
    label = MyGlobal.checkNullOrEmpty(json['label']);
    name = MyGlobal.checkNullOrEmpty(json['name']);

    var optionsList = json['options'] as List;
    for (int iIndex = 0; iIndex < optionsList.length; iIndex++) {
      String optionsStr = optionsList[iIndex];
      //print("optionsStr:$optionsStr");
      options.add(optionsStr);
    }
  }
}

//-------------------nodes Model
class NodelModel {
  String label = "";
  String name = "";
  String value = "";

  NodelModel({this.label = "", this.name = "", this.value = ""});

  NodelModel.fromJson(Map<String, dynamic> json) {
    label = MyGlobal.checkNullOrEmpty(json['label']);
    name = MyGlobal.checkNullOrEmpty(json['name']);
    value = MyGlobal.checkNullOrEmpty(json['value']);
  }
}

//-------------------variations Model
class VariationsModel {
  String stockStatus = "";
  int stockQuantity = 0;
  int databaseId = 0;

  String price = "";
  String regularPrice = "";
  String slug = "";
  String link = "";
  String sourceUrl = "";

  List<NodelModel> nodModelList = [];

  String tagKey = "";

  VariationsModel({
    this.stockStatus = "",
    this.stockQuantity = 0,
    this.databaseId = 0,
    this.price = "",
    this.regularPrice = "",
    this.slug = "",
    this.link = "",
    this.sourceUrl = "",
    this.nodModelList = const [],
    this.tagKey = "",
  });

  VariationsModel.fromJson(Map<String, dynamic> json) {
    stockStatus = MyGlobal.checkNullOrEmpty(json['stockStatus']);
    stockQuantity = MyGlobal.checkNullOrZero(json['stockQuantity']);
    databaseId = MyGlobal.checkNullOrZero(json['databaseId']);

    price = MyGlobal.checkNullOrEmpty(json['price']);
    regularPrice = MyGlobal.checkNullOrEmpty(json['price']);
    slug = MyGlobal.checkNullOrEmpty(json['price']);
    link = MyGlobal.checkNullOrEmpty(json['price']);
    sourceUrl = MyGlobal.checkNullOrEmpty(json['image']["sourceUrl"]);

    //------
    String strKey = "";
    var nodesList = json["attributes"]['nodes'] as List;
    for (int iIndex = 0; iIndex < nodesList.length; iIndex++) {
      NodelModel nodMol = NodelModel.fromJson(nodesList[iIndex]);
      strKey = "$strKey${nodMol.value}";
      //print("strKey:$strKey");
      nodModelList.add(nodMol);
    }

    tagKey = strKey;
    //print("tagKey:$tagKey");
  }
}
